package hr.bean;

import hr.dao.AttendanceTypeDAO;

import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.pojo.hr_attendance_type;
import resources.util.Tool;

@ManagedBean(name="attendanceType")
@SessionScoped
public class AttendanceType {
	private int currentPage;
	private hr_attendance_type attendanceType;

	private hr_attendance_type selectedAttendanceType;
	private String nameSearch;
	private ArrayList<hr_attendance_type> listAttendanceType;
	private Integer[] pages;
	private String last;
	
	public void onload() {
		if(!Tool.isPostBack()){
			init();
		}
	}
	public void initObject(){
		attendanceType = new hr_attendance_type();
	}
	
	public void init(){
		currentPage = 1;
		nameSearch="";
		attendanceType = new hr_attendance_type();
		listAttendanceType = AttendanceTypeDAO.getListAttendanceTypePaging("",currentPage,Tool.max_row);
		setPages(Tool.getPages());
	}
	
	public void paging(){
		listAttendanceType = AttendanceTypeDAO.getListAttendanceTypePaging("",currentPage,Tool.max_row);
		setPages(Tool.getPages());
	}
	
	public void insert(){
		attendanceType.setId("ATE"+Tool.generateID());
		attendanceType.setStatus(1);
		String result = AttendanceTypeDAO.action("insert",attendanceType,0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
		}
		init();
	}
	
	public void update(){
		String result = AttendanceTypeDAO.action("update",selectedAttendanceType,last.equals(selectedAttendanceType.getName())?1:0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
			selectedAttendanceType = new hr_attendance_type();
		}
		init();
	}
	
	public void choose(ActionEvent event){
		selectedAttendanceType = (hr_attendance_type) event.getComponent().getAttributes().get("object");
		last=selectedAttendanceType.getName();
	}
	
	public void search(){
		listAttendanceType = AttendanceTypeDAO.getListAttendanceTypePaging(nameSearch,1,Tool.max_row);
		setPages(Tool.getPages());
	}
	
	public void nextPage(){
		currentPage ++;
	}

	public String getNameSearch() {
		return nameSearch;
	}

	public void setNameSearch(String nameSearch) {
		this.nameSearch = nameSearch;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}

	public hr_attendance_type getAttendanceType() {
		return attendanceType;
	}

	public void setAttendanceType(hr_attendance_type attendanceType) {
		this.attendanceType = attendanceType;
	}

	public hr_attendance_type getSelectedAttendanceType() {
		return selectedAttendanceType;
	}

	public void setSelectedAttendanceType(hr_attendance_type selectedAttendanceType) {
		this.selectedAttendanceType = selectedAttendanceType;
	}

	public ArrayList<hr_attendance_type> getListAttendanceType() {
		return listAttendanceType;
	}

	public void setListAttendanceType(
			ArrayList<hr_attendance_type> listAttendanceType) {
		this.listAttendanceType = listAttendanceType;
	}

}
